<?xml version="1.0"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>SASAPI</title><script src="../../../quicknav.js" type="text/javascript"></script><script><!--
document.quicknavBasePath="../../../index-files";
//--></script><link href="../../../style.css" title="JavaDoc" rel="stylesheet" type="text/css"/><link href="../../../unnatural.css" title="Unnatural" rel="alternate stylesheet" type="text/css"/><link href="../../../overview-summary.html" title="Overview" rel="top"/><meta name="generator" content="http://www.badgers-in-foil.co.uk/projects/as2api/"/></head><body><pre><span class="lineno" id="1">     1  </span><span class="comment doc">/**
<span class="lineno" id="2">     2  </span> * com.sekati.utils.ClassUtils
<span class="lineno" id="3">     3  </span> * @version 2.2.0
<span class="lineno" id="4">     4  </span> * @author jason m horwitz | sekati.com
<span class="lineno" id="5">     5  </span> * Copyright (C) 2007  jason m horwitz, Sekat LLC. All Rights Reserved.
<span class="lineno" id="6">     6  </span> * Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
<span class="lineno" id="7">     7  </span> */</span>
<span class="lineno" id="8">     8  </span> 
<span class="lineno" id="9">     9  </span><span class="key">import</span> com.sekati.core.KeyFactory;
<span class="lineno" id="10">    10  </span>
<span class="lineno" id="11">    11  </span><span class="comment doc">/**
<span class="lineno" id="12">    12  </span> * Static class for wrapping various Class utilities. For example linking 'extend MovieClip' type 
<span class="lineno" id="13">    13  </span> * classes to MovieClips thru attachMovie, createEmptyMovieClip or MovieClip Instances on stage.&lt;br/&gt;&lt;br/&gt;
<span class="lineno" id="14">    14  </span> * 
<span class="lineno" id="15">    15  </span> * An initObject param is available in methods: {@link #createEmptyMovieClip}, {@link #attachMovie}
<span class="lineno" id="16">    16  </span> * and {@link #attachClass}.  _depth is a custom initObject param which will set the clip to this depth 
<span class="lineno" id="17">    17  </span> * regardless of method but *will not* store _depth as a MovieClip property; use getDepth if needed.
<span class="lineno" id="18">    18  </span> *
<span class="lineno" id="19">    19  </span> * {@code Example Class:
<span class="lineno" id="20">    20  </span> * class com.sekati.Test extends MovieClip {
<span class="lineno" id="21">    21  </span> * 	public function Test(){
<span class="lineno" id="22">    22  </span> * 		trace("Test Class instantiated on: "+this._name);
<span class="lineno" id="23">    23  </span> * 	}
<span class="lineno" id="24">    24  </span> * }
<span class="lineno" id="25">    25  </span> * }
<span class="lineno" id="26">    26  </span> * 
<span class="lineno" id="27">    27  </span> * @TODO Add an AS2 Class index getter for debugging purposes.
<span class="lineno" id="28">    28  </span> */</span>
<span class="lineno" id="29">    29  </span><span class="key">class</span> com.sekati.utils.ClassUtils {
<span class="lineno" id="30">    30  </span>
<span class="lineno" id="31">    31  </span>	<span class="comment doc">/**
<span class="lineno" id="32">    32  </span>	 * create a movieclip with linked class (various init options)
<span class="lineno" id="33">    33  </span>	 * @param classRef (Function) reference to class to attach
<span class="lineno" id="34">    34  </span>	 * @param target (MovieClip) target scope to create MovieClip
<span class="lineno" id="35">    35  </span>	 * @param instanceName (String) created MovieClip instance name
<span class="lineno" id="36">    36  </span>	 * @param initObject (Object) object of properties to create MovieClip with. Depth will automatically be created if none is specified
<span class="lineno" id="37">    37  </span>	 * @return MovieClip
<span class="lineno" id="38">    38  </span>	 * {@code Usage:
<span class="lineno" id="39">    39  </span>	 * var mc0:MovieClip = ClassUtils.createEmptyMovieClip (com.sekati.Test, this, "mc0");
<span class="lineno" id="40">    40  </span>	 * var mc0:MovieClip = ClassUtils.createEmptyMovieClip (com.sekati.Test, _root, "mc0", {_depth: 100, _x:25, _y:25});
<span class="lineno" id="41">    41  </span>	 * var mc0:MovieClip = ClassUtils.createEmptyMovieClip (com.sekati.Test, this, "mc0", {_x:25, _y:25});
<span class="lineno" id="42">    42  </span>	 * }
<span class="lineno" id="43">    43  </span>	 */</span>
<span class="lineno" id="44">    44  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> createEmptyMovieClip(classRef:Function, target:MovieClip, instanceName:String, initObject:Object):MovieClip {
<span class="lineno" id="45">    45  </span>		<span class="key">var</span> depth:Number = (!initObject._depth) ? target.getNextHighestDepth( ) : initObject._depth;
<span class="lineno" id="46">    46  </span>		<span class="key">var</span> mc:MovieClip = target.createEmptyMovieClip( instanceName, depth );
<span class="lineno" id="47">    47  </span>		mc.__proto__ = classRef.prototype;
<span class="lineno" id="48">    48  </span>		<span class="key">if</span> (initObject) {
<span class="lineno" id="49">    49  </span>			<span class="key">for</span> (<span class="key">var</span> i <span class="key">in</span> initObject) {
<span class="lineno" id="50">    50  </span>				<span class="key">if</span> (i != <span class="str_const">"_depth"</span>) {
<span class="lineno" id="51">    51  </span>					mc[i] = initObject[i];
<span class="lineno" id="52">    52  </span>				}
<span class="lineno" id="53">    53  </span>			}
<span class="lineno" id="54">    54  </span>		}
<span class="lineno" id="55">    55  </span>		classRef.apply( mc );
<span class="lineno" id="56">    56  </span>		KeyFactory.inject( mc );
<span class="lineno" id="57">    57  </span>		<span class="key">return</span> mc;
<span class="lineno" id="58">    58  </span>	}	
<span class="lineno" id="59">    59  </span>
<span class="lineno" id="60">    60  </span>	<span class="comment doc">/**
<span class="lineno" id="61">    61  </span>	 * attach a MovieClip from library and extend with class (various init options)
<span class="lineno" id="62">    62  </span>	 * @param classRef (Function) reference to class to attach
<span class="lineno" id="63">    63  </span>	 * @param target (MovieClip) target scope to create MovieClip
<span class="lineno" id="64">    64  </span>	 * @param idName (String) linkage id for exported MovieClip in library	
<span class="lineno" id="65">    65  </span>	 * @param instanceName (String) created MovieClip instance name
<span class="lineno" id="66">    66  </span>	 * @param initObject (Object) object of properties to create MovieClip with. Depth will automatically be created if none is specified
<span class="lineno" id="67">    67  </span>	 * @return MovieClip
<span class="lineno" id="68">    68  </span>	 * {@code Usage:
<span class="lineno" id="69">    69  </span>	 * var mc1:MovieClip = ClassUtils.attachMovie (com.sekati.Test, _root, "linkedMc", "mc1");
<span class="lineno" id="70">    70  </span>	 * var mc1:MovieClip = ClassUtils.attachMovie (com.sekati.Test, _root, "linkedMc", "mc1", {_x:50, _y:50});
<span class="lineno" id="71">    71  </span>	 * var mc1:MovieClip = ClassUtils.attachMovie (com.sekati.Test, _root, "linkedMc", "mc1", {_depth:200, _x:50, _y:50});
<span class="lineno" id="72">    72  </span>	 * }
<span class="lineno" id="73">    73  </span>	 */</span>
<span class="lineno" id="74">    74  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> attachMovie(classRef:Function, target:MovieClip, idName:String, instanceName:String, initObject:Object):MovieClip {
<span class="lineno" id="75">    75  </span>		<span class="key">var</span> depth:Number = (!initObject._depth) ? target.getNextHighestDepth( ) : initObject._depth;
<span class="lineno" id="76">    76  </span>		<span class="key">var</span> mc:MovieClip = target.attachMovie( idName, instanceName, depth, initObject );
<span class="lineno" id="77">    77  </span>		mc.__proto__ = classRef.prototype;
<span class="lineno" id="78">    78  </span>		<span class="key">if</span> (mc._depth) {
<span class="lineno" id="79">    79  </span>			<span class="key">delete</span> mc._depth;
<span class="lineno" id="80">    80  </span>		}
<span class="lineno" id="81">    81  </span>		classRef.apply( mc );
<span class="lineno" id="82">    82  </span>		KeyFactory.inject( mc );
<span class="lineno" id="83">    83  </span>		<span class="key">return</span> mc;
<span class="lineno" id="84">    84  </span>	}
<span class="lineno" id="85">    85  </span>
<span class="lineno" id="86">    86  </span>	<span class="comment doc">/**
<span class="lineno" id="87">    87  </span>	 * Attach a movie from a DLL swf's library (loads dll then attaches requested movie).
<span class="lineno" id="88">    88  </span>	 * NOTE: you should insert a delay between your callback and methods calls in the shared library as it initializes.
<span class="lineno" id="89">    89  </span>	 * @param dll (Function) url of the dll.swf which contains exported assets in its library
<span class="lineno" id="90">    90  </span>	 * @param target (MovieClip) target scope to attachMovie within
<span class="lineno" id="91">    91  </span>	 * @param idName (String) linkage id for exported MovieClip in library	
<span class="lineno" id="92">    92  </span>	 * @param instanceName (String) created MovieClip instance name
<span class="lineno" id="93">    93  </span>	 * @param initObject (Object) object of properties to create MovieClip with. Depth will automatically be created if none is specified
<span class="lineno" id="94">    94  </span>	 * @param cb (Function) callback function to fire when dll has been loaded and clip attached
<span class="lineno" id="95">    95  </span>	 * @return MovieClip
<span class="lineno" id="96">    96  </span>	 * {@code Usage:
<span class="lineno" id="97">    97  </span>	 * var mc0:MovieClip = ClassUtils.attachDllMovie("dll.swf", _root, "myDllExportedItem", "mc0", {_x:50, _y:50, _depth:20}, myCallBackFn);
<span class="lineno" id="98">    98  </span>	 * }
<span class="lineno" id="99">    99  </span>	 */</span>
<span class="lineno" id="100">   100  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> attachDllMovie(dll:String, target:MovieClip, idName:String, instanceName:String, initObject:Object, cb:Function):MovieClip {
<span class="lineno" id="101">   101  </span>		<span class="key">var</span> depth:Number = (!initObject._depth) ? target.getNextHighestDepth( ) : initObject._depth;
<span class="lineno" id="102">   102  </span>		<span class="key">var</span> mc:MovieClip = target.createEmptyMovieClip( instanceName, depth );
<span class="lineno" id="103">   103  </span>		<span class="key">var</span> mcLoader:MovieClipLoader = <span class="key">new</span> MovieClipLoader( );
<span class="lineno" id="104">   104  </span>		<span class="key">var</span> onDLLLoaded:Function = <span class="key">function</span>():Void {
<span class="lineno" id="105">   105  </span>			mcLoader.removeListener( listener );
<span class="lineno" id="106">   106  </span>			<span class="key">if</span>(cb) _global[<span class="str_const">"setTimeout"</span>]( <span class="key">this</span>, <span class="str_const">"cb"</span>, <span class="num_const">50</span> );
<span class="lineno" id="107">   107  </span>		};
<span class="lineno" id="108">   108  </span>		<span class="key">var</span> listener:Object = <span class="key">new</span> Object( );
<span class="lineno" id="109">   109  </span>		listener.onLoadInit = <span class="key">function</span>(mc:MovieClip):Void {
<span class="lineno" id="110">   110  </span>			mc.attachMovie( idName, instanceName, mc.getNextHighestDepth( ) );
<span class="lineno" id="111">   111  </span>			target[instanceName] = target[instanceName][instanceName];
<span class="lineno" id="112">   112  </span>			<span class="key">if</span> (initObject) {
<span class="lineno" id="113">   113  </span>				<span class="key">for</span> (<span class="key">var</span> i <span class="key">in</span> initObject) {
<span class="lineno" id="114">   114  </span>					<span class="key">if</span> (i != <span class="str_const">"_depth"</span>) mc[i] = initObject[i];
<span class="lineno" id="115">   115  </span>				}
<span class="lineno" id="116">   116  </span>			}				
<span class="lineno" id="117">   117  </span>			onDLLLoaded( );
<span class="lineno" id="118">   118  </span>		};
<span class="lineno" id="119">   119  </span>		mcLoader.addListener( listener );
<span class="lineno" id="120">   120  </span>		mcLoader.loadClip( dll, mc );
<span class="lineno" id="121">   121  </span>		<span class="key">return</span> mc;
<span class="lineno" id="122">   122  </span>	}	
<span class="lineno" id="123">   123  </span>
<span class="lineno" id="124">   124  </span>	<span class="comment doc">/**
<span class="lineno" id="125">   125  </span>	 * extend a MovieClip instance (on stage) with class (various init options)
<span class="lineno" id="126">   126  </span>	 * @param classRef (Function) reference to class to attach
<span class="lineno" id="127">   127  </span>	 * @param target (MovieClip) target scope to create MovieClip
<span class="lineno" id="128">   128  </span>	 * @param initObject (Object) object of properties to create MovieClip with. Depth will automatically be created if none is specified
<span class="lineno" id="129">   129  </span>	 * @return MovieClip
<span class="lineno" id="130">   130  </span>	 * {@code Usage:
<span class="lineno" id="131">   131  </span>	 * var mc2:MovieClip = ClassUtils.attachClass (com.sekati.Test, mc2);
<span class="lineno" id="132">   132  </span>	 * var mc2:MovieClip = ClassUtils.attachClass (com.sekati.Test, _root.mc2, {_x:75, _y:75});
<span class="lineno" id="133">   133  </span>	 * var mc2:MovieClip = ClassUtils.attachClass (com.sekati.Test, mc2, {_depth:300, _x:75, _y:75});
<span class="lineno" id="134">   134  </span>	 * }
<span class="lineno" id="135">   135  </span>	 */</span>
<span class="lineno" id="136">   136  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> attachClass(classRef:Function, target:MovieClip, initObject:Object):MovieClip {
<span class="lineno" id="137">   137  </span>		<span class="key">var</span> mc:MovieClip = target;
<span class="lineno" id="138">   138  </span>		mc.__proto__ = classRef.prototype;
<span class="lineno" id="139">   139  </span>		<span class="key">if</span> (initObject) {
<span class="lineno" id="140">   140  </span>			<span class="key">for</span> (<span class="key">var</span> i <span class="key">in</span> initObject) {
<span class="lineno" id="141">   141  </span>				<span class="key">if</span> (i != <span class="str_const">"_depth"</span>) {
<span class="lineno" id="142">   142  </span>					mc[i] = initObject[i];
<span class="lineno" id="143">   143  </span>				} <span class="key">else</span> {
<span class="lineno" id="144">   144  </span>					target.swapDepths( initObject[i] );
<span class="lineno" id="145">   145  </span>				}
<span class="lineno" id="146">   146  </span>			}
<span class="lineno" id="147">   147  </span>		}
<span class="lineno" id="148">   148  </span>		classRef.apply( mc );
<span class="lineno" id="149">   149  </span>		KeyFactory.inject( mc );
<span class="lineno" id="150">   150  </span>		<span class="key">return</span> target;
<span class="lineno" id="151">   151  </span>	}
<span class="lineno" id="152">   152  </span>
<span class="lineno" id="153">   153  </span>	<span class="comment doc">/**
<span class="lineno" id="154">   154  </span>	 * Create and return a new instance of a defined class
<span class="lineno" id="155">   155  </span>	 * @param classRef (Function) reference to full class namespace
<span class="lineno" id="156">   156  </span>	 * @param args (Array) array of constructor arguments
<span class="lineno" id="157">   157  </span>	 * @return Object - instantiated class object
<span class="lineno" id="158">   158  </span>	 * {@code Usage:
<span class="lineno" id="159">   159  </span>	 * var o:Point = ClassUtils.createInstance (com.sekati.geom.Point, [15,50]);
<span class="lineno" id="160">   160  </span>	 * }
<span class="lineno" id="161">   161  </span>	 */</span>
<span class="lineno" id="162">   162  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> createInstance(classRef:Function, args:Array):Object {
<span class="lineno" id="163">   163  </span>		<span class="key">var</span> o:Object = {__constructor__:classRef, __proto__:classRef.prototype};
<span class="lineno" id="164">   164  </span>		classRef.apply( o, args );
<span class="lineno" id="165">   165  </span>		KeyFactory.inject( o );
<span class="lineno" id="166">   166  </span>		<span class="key">return</span> o;
<span class="lineno" id="167">   167  </span>	}
<span class="lineno" id="168">   168  </span>
<span class="lineno" id="169">   169  </span>	<span class="comment doc">/**
<span class="lineno" id="170">   170  </span>	 *  Create and return a new instance of a defined class without 
<span class="lineno" id="171">   171  </span>	 *  invoking its constructor
<span class="lineno" id="172">   172  </span>	 *  @param classRef (Function) reference to full class namespace
<span class="lineno" id="173">   173  </span>	 *  @return Object - class object
<span class="lineno" id="174">   174  </span>	 *  {@code Usage:
<span class="lineno" id="175">   175  </span>	 *  var scr:Scroll = ClassUtils.createCleanInstance(com.sekati.ui.Scroll);
<span class="lineno" id="176">   176  </span>	 *  }
<span class="lineno" id="177">   177  </span>	 */</span>
<span class="lineno" id="178">   178  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> createCleanInstance(classRef:Function):Object {
<span class="lineno" id="179">   179  </span>		<span class="key">var</span> o:Object = <span class="key">new</span> Object;
<span class="lineno" id="180">   180  </span>		o.__proto__ = classRef.prototype;
<span class="lineno" id="181">   181  </span>		o.__constructor__ = classRef;
<span class="lineno" id="182">   182  </span>		KeyFactory.inject( o );
<span class="lineno" id="183">   183  </span>		<span class="key">return</span> o;	
<span class="lineno" id="184">   184  </span>	}
<span class="lineno" id="185">   185  </span>
<span class="lineno" id="186">   186  </span>	<span class="comment doc">/**
<span class="lineno" id="187">   187  </span>	 * Check if a subclass is extended by a superclass
<span class="lineno" id="188">   188  </span>	 * @param subclassRef (Function) reference to the full subclass namespace
<span class="lineno" id="189">   189  </span>	 * @param superclassRef (Function) reference to the full superclass namespace
<span class="lineno" id="190">   190  </span>	 * @return Boolean
<span class="lineno" id="191">   191  </span>	 * {@code Usage:
<span class="lineno" id="192">   192  </span>	 * 	trace(ClassUtils.isSubclassOf(com.sekati.display.AbstractClip, com.sekati.display.CoreClip)); // returns: true
<span class="lineno" id="193">   193  </span>	 * }
<span class="lineno" id="194">   194  </span>	 */</span>
<span class="lineno" id="195">   195  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> isSubclassOf(subclassRef:Function, superclassRef:Function):Boolean {
<span class="lineno" id="196">   196  </span>		<span class="key">var</span> o:Object = subclassRef.prototype;
<span class="lineno" id="197">   197  </span>		<span class="key">while</span>(o !== undefined) {
<span class="lineno" id="198">   198  </span>			o = o.__proto__;
<span class="lineno" id="199">   199  </span>			<span class="key">if</span>(o === superclassRef.prototype) {
<span class="lineno" id="200">   200  </span>				<span class="key">return</span> <span class="key">true</span>;
<span class="lineno" id="201">   201  </span>			}	
<span class="lineno" id="202">   202  </span>		}
<span class="lineno" id="203">   203  </span>		<span class="key">return</span> <span class="key">false</span>;
<span class="lineno" id="204">   204  </span>	}
<span class="lineno" id="205">   205  </span>
<span class="lineno" id="206">   206  </span>	<span class="comment doc">/**
<span class="lineno" id="207">   207  </span>	 * Check if a class implements an interface
<span class="lineno" id="208">   208  </span>	 * @param classRef (Function) reference to the full class namespace
<span class="lineno" id="209">   209  </span>	 * @param interfaceRef (Function) reference to the full interface namespace
<span class="lineno" id="210">   210  </span>	 * @return Boolean
<span class="lineno" id="211">   211  </span>	 * {@code Usage:
<span class="lineno" id="212">   212  </span>	 * 	trace(ClassUtils.isImplementationOf(com.sekati.display.CoreClip, com.sekati.display.ICoreClip)); // returns: true
<span class="lineno" id="213">   213  </span>	 * }
<span class="lineno" id="214">   214  </span>	 */</span>
<span class="lineno" id="215">   215  </span>	<span class="key">public</span> <span class="key">static</span> <span class="key">function</span> isImplementationOf(classRef:Function, interfaceRef:Function):Boolean {
<span class="lineno" id="216">   216  </span>		<span class="comment">// interface will not be in prototype chain</span>
<span class="lineno" id="217">   217  </span>		<span class="key">if</span>(isSubclassOf( classRef, interfaceRef )) {
<span class="lineno" id="218">   218  </span>			<span class="key">return</span> <span class="key">false</span>;	
<span class="lineno" id="219">   219  </span>		}
<span class="lineno" id="220">   220  </span>		<span class="comment">// if an instance it is not extended, the class has to be an instance of it</span>
<span class="lineno" id="221">   221  </span>		<span class="key">return</span> (createCleanInstance( classRef ) <span class="key">instanceof</span> interfaceRef);
<span class="lineno" id="222">   222  </span>	}
<span class="lineno" id="223">   223  </span>
<span class="lineno" id="224">   224  </span>	<span class="key">private</span> <span class="key">function</span> ClassUtils() {
<span class="lineno" id="225">   225  </span>	}
<span class="lineno" id="226">   226  </span>}</pre><ul class="main_nav" id="main_nav"><li><a href="../../../overview-summary.html" title="Overview of SASAPI" class="button">Overview</a></li><li><a href="package-summary.html" title="Overview of package com.sekati.utils" class="button">Package</a></li><li><a href="ClassUtils.html" title="Detail of com.sekati.utils.ClassUtils API" class="button">Class</a></li><li><span class="button nav_current">Source</span></li><li><a href="../../../index-files/index.html" title="Alpabetical index of types and members" class="button">Index</a></li></ul><div class="footer"><a href="http://www.badgers-in-foil.co.uk/projects/as2api/" title="ActionScript 2 API Documentation Generator">as2api</a></div></body></html>